package com.sy.server.service.impl;

import com.sy.server.service.IFeedbackService;
import com.sy.server.util.MyConst;
import com.sy.server.dao.RedisDAO;
import com.sy.server.mapper.FeedbackMapper;
import com.sy.server.model.Feedback;
import com.sy.server.model.FeedbackExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service
public class FeedbackService implements IFeedbackService {
    private final Logger logger= LoggerFactory.getLogger(FeedbackService.class);


    @Autowired
    private FeedbackMapper mapper;
    @Autowired
    private RedisDAO rdao;

    @Autowired
    private IpService ipService;


    public List<Feedback> queryAll() {
        FeedbackExample example=new FeedbackExample();
        example.createCriteria().andFIdIsNotNull();
        List<Feedback> list = mapper.selectByExampleWithBLOBs(example);
        list.forEach(feedback -> feedback.setLikes(praiseCount(feedback.getfId().toString())));
        return list;
    }

    public boolean praise(String fid) {
        Integer userId=ipService.getUserIdByCurrentIp();
        boolean hasPraised=rdao.getBit(MyConst.FEEDBACK_PREFIX+fid,userId.longValue());
        rdao.setBit(MyConst.FEEDBACK_PREFIX+fid,userId,!hasPraised);
        if(!hasPraised){
            logger.info("用户【{}】点赞了评论【{}】",userId,fid);
        }else{
            logger.info("用户【{}】取消了对评论【{}】的点赞",userId,fid);
        }
        return hasPraised;
    }

    public Long praiseCount(String fid) {
        Integer userId=ipService.getUserIdByCurrentIp();
        return rdao.bitCount(MyConst.FEEDBACK_PREFIX+fid);
    }

    public void add(Feedback feedback) {
        Integer userId=ipService.getUserIdByCurrentIp();
        feedback.setfUserId(userId);
        feedback.setLikes(0L);
        feedback.setSendTime(new Date());
        mapper.insert(feedback);
        logger.info("用户【{}】新增一条反馈",userId);
    }

    public boolean checkIfPraised(String fid){
        Integer userId=ipService.getUserIdByCurrentIp();
        return rdao.getBit(MyConst.FEEDBACK_PREFIX+fid,userId.longValue());
    }

    public void syncLikes() {
        logger.info("开始同步点赞数据");
        Set set=rdao.keys(MyConst.FEEDBACK_PREFIX+"*");
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String feedbackKey=(String)iterator.next();
            String fid=feedbackKey.substring(feedbackKey.indexOf("-")+1);
            Long likes=rdao.bitCount(feedbackKey);
            Feedback feedback=new Feedback();
            feedback.setfId(Integer.parseInt(fid));
            feedback.setLikes(likes);
            mapper.updateLikesByPrimaryKey(feedback);
        }
        logger.info("同步点赞数完成");
    }

    public boolean checkIfComment(String fid) {
        Integer userId=ipService.getUserIdByCurrentIp();
        FeedbackExample example=new FeedbackExample();
        example.createCriteria().andFIdEqualTo(Integer.parseInt(fid)).andFUserIdEqualTo(userId);
        return mapper.countByExample(example) > 0;
    }

    public void del(String fid) {
        Integer userId=ipService.getUserIdByCurrentIp();
        mapper.deleteByPrimaryKey(Integer.parseInt(fid));
        logger.info("用户【{}】删除了评论【{}】",userId,fid);
    }
}
